package com.soundShape.utils
{
	import com.soundShape.vo.PhotoVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;

	public class ThumbnailCreator
	{
		public function ThumbnailCreator()
		{
		}
		
		private var ndx:int;
		
		private var _photoVOs:ArrayCollection;
		
		private var _callback:Function;
		
		private var loader:Loader;
		
		private var imgBitmaps:Array;
		
		private var _treatmentID:int;
		
		private var _comments:String;
		
		public function createThumbnails(photoVOs:ArrayCollection, callback:Function):void
		{
			ndx = 0;
			
			imgBitmaps = [];
			
			_photoVOs = photoVOs;
			
			_callback = callback;
			
			createImage();
		}
		
		
		
		private function createImage():void
		{
			loader = new Loader();
			var photoVO:PhotoVO = PhotoVO(_photoVOs.getItemAt(ndx));
			_treatmentID = photoVO.treatmentID;
			_comments = photoVO.comments;
			var urlReq:URLRequest = new URLRequest( photoVO.path );
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaded);
			loader.load(urlReq);
		}
		
		private function handleImageLoaded(event:Event):void
		{
			var bmp:Bitmap = new Bitmap();
			bmp = loader.content as Bitmap;
			
			imgBitmaps.push({imgSource:bmp, treatmentID:_treatmentID, comments:_comments});
			
			ndx++;
			
			trace(ndx + " " + _photoVOs.length);
			
			if(ndx > _photoVOs.length-1)
				_callback.call(null,imgBitmaps);
			else
				createImage();
			
		}
	}
}